********Table R.1 - Indonesia********

eststo clear


*Load data and create differences
cd "$reploc/data/created_data"
use indo_full, clear

sort pidlink ifls
drop if ifls==.
keep if panel==1

keep rbuc*  gambleaverse ifls pidlink inflation imp_age panel biryear_impl grbr???? indo_gr????  birprov_impl grbrprovAvg_lam0co3 grbrprovSD_lam0co3


reshape wide rbuc*  gambleaverse inflation imp_age grbrprovAvg_lam0co3 grbrprovSD_lam0co3, i(pidlink) j(ifls)


local varlist1 rbuc rbucB rbuc_nga rbuc_garefined rbucgaref1a rbucgaref2a rbucgaref3a rbucgaref4a gambleaverse inflation grbrprovAvg_lam0co3 grbrprovSD_lam0co3


foreach var in `varlist1'  {
gen `var'dif=.
replace `var'dif=`var'5-`var'4
}

*Generate cohort by birthyear variables to cluster on
egen coh_prov_clust=group(biryear_impl birprov_impl)

*Do this for primary sample
gen primarysample1=0
replace primarysample1=1 if rbuc_garefineddif!=. & grbrprovAvg_lam0co3dif!=. & grbrprovSD_lam0co3dif!=. & inflationdif!=. &  biryear_impl>=1961 & coh_prov_clust!=.

egen primarysample=total(primarysample1), by(pidlink)

keep if primarysample==1


*Regressions
local x rbuc_garefined
local rhsvari oldindoonly
local rhsvaruse co3
local usei grbr

*Lambda value seeds are obtained by maximizing the likelihood obtained by estimating linearized versions of the model with different values of lambda. The code to find these seeds is available upon request
local lam1vec 440 N 3950
local lam2vec N 4140 3950

drop if `x'dif==.

forval subreg=1/3 {

preserve

local lam1int : word `subreg' of `lam1vec'
local lam2int : word `subreg' of `lam2vec'

local hola1=substr("`lam1int'",1,3)
local hola2=substr("`lam2int'",1,3)

if "`lam1int'"!="N" & "`hola1'"!="neg" {
local lam1=round(`lam1int'/100,0.1)
}

if "`lam2int'"!="N" & "`hola2'"!="neg" {
local lam2=round(`lam2int'/100,0.1)
}

if "`lam1int'"!="N" & "`hola1'"=="neg" {
local lam1inta=substr("`lam1int'",4,.)
local lam1=(-1)*round(`lam1inta'/100,0.1)
}

if "`lam2int'"!="N" & "`hola2'"=="neg" {
local lam2inta=substr("`lam2int'",4,.)
local lam2=(-1)*round(`lam2inta'/100,0.1)
}

*Province growth
local allgrs `usei'1977
forval i=1978/2014 {
local allgrs `allgrs' `usei'`i'
}


*Indonesia growth
local allgrsindo indo_gr1961
forval i=1962/2014 {
local allgrsindo `allgrsindo' indo_gr`i'
}

*Column 1
if `subreg'==1 {

gen mis=missing(inflationdif,imp_age4,imp_age5,biryear_impl,`x'dif)
drop if mis==1

foreach v of varlist grbr???? { 
drop if missing(`v') 
}

cd "$reploc/programs"
eststo: nl lambda_spec1_`rhsvaruse' @ `x'dif `allgrs' inflationdif imp_age4 imp_age5 biryear_impl `allgrsindo' if panel==1, parameters(bcons bgr lam binf) initial(bcons 0 bgr 0 lam `lam1' binf 0) cluster(coh_prov_clust)

}

*Column 2
if `subreg'==2 {

gen mis=missing(inflationdif,imp_age4,imp_age5,biryear_impl,`x'dif)
drop if mis==1
foreach v of varlist grbr???? { 
	drop if missing(`v') 
}

cd "$reploc/programs"
eststo: nl lambda_spec2_`rhsvaruse' @ `x'dif `allgrs' inflationdif imp_age4 imp_age5 biryear_impl `allgrsindo' if panel==1, parameters(bcons bsd lam binf) initial(bcons 0 bsd 0 lam `lam2' binf 0) cluster(coh_prov_clust)

}

*Column 3
if `subreg'==3 {
gen mis=missing(inflationdif,imp_age4,imp_age5,biryear_impl,`x'dif)
drop if mis==1
foreach v of varlist grbr???? { 
	drop if missing(`v') 
}

cd "$reploc/programs"
eststo: nl lambda_spec3_`rhsvaruse' @ `x'dif `allgrs' inflationdif imp_age4 imp_age5 biryear_impl `allgrsindo'  if panel==1, parameters(bcons bgr bsd lam binf) initial(bcons 0 bgr 0 bsd 0 lam `lam1' binf 0) cluster(coh_prov_clust)

}

restore

}

cd "$reploc/results"	
esttab using TableR1_Indonesia.tex, star(* 0.1 ** 0.05 *** 0.01) b(2) se(2) replace
